package cn.mw.cmdb.view;

import cn.mw.cmdb.entity.AssetsScanTotalResultInfo;
import cn.mw.cmdb.entity.AssetsScanTotalResultInfoVo;
import cn.mw.cmdb.entity.ScanResultFailInfo;
import cn.mw.cmdb.entity.ScanResultSuccessInfo;
import cn.mw.cmdb.param.AssetsInstanceScanSearchParam;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.page.MWPage;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class AssetsScanTotalResultConverter implements ViewConvert<AssetsScanTotalResultInfo, Object> {
    public static String SearchParamKey = "AssetsScanResultSearchParam";
    private String successSign = "success";
    private String noInsertSign = "noInsert";
    private String insertSign = "insert";

    private String failSign = "fail";

    @Override
    public AssetsScanTotalResultInfoVo convert(AssetsScanTotalResultInfo assetsScanResultInfo, ViewConvertContext context) throws Exception {
        AssetsScanTotalResultInfoVo infoVo = new AssetsScanTotalResultInfoVo();
        AssetsInstanceScanSearchParam searchParam = (AssetsInstanceScanSearchParam) context.get(SearchParamKey);
        String queryIdent = searchParam.getQueryIdent();
        MWPage mwPage = searchParam.getPage();
        infoVo.setId(assetsScanResultInfo.getId());
        mwPage.setTotal(-1);
        if (!StringUtils.hasText(queryIdent) || noInsertSign.equals(queryIdent)) {
            List<ScanResultSuccessInfo> noInsertList = new ArrayList<>();
            List<ScanResultSuccessInfo> ret = assetsScanResultInfo.getSuccessList();
            for(ScanResultSuccessInfo successInfo : ret){
                if(!successInfo.getExist()){
                    noInsertList.add(successInfo);
                }
            }
            List<ScanResultSuccessInfo> noInsertListPage = process(noInsertList, ScanResultSuccessInfo.class, searchParam);
            infoVo.setNoInsertList(noInsertListPage);
            infoVo.setFailList(null);
        }else if(!StringUtils.hasText(queryIdent) || insertSign.equals(queryIdent)){
            List<ScanResultSuccessInfo> insertList = new ArrayList<>();
            List<ScanResultSuccessInfo> ret = assetsScanResultInfo.getSuccessList();
            for(ScanResultSuccessInfo successInfo : ret){
                if(successInfo.getExist()){
                    insertList.add(successInfo);
                }
            }
            List<ScanResultSuccessInfo> insertListPage = process(insertList, ScanResultSuccessInfo.class, searchParam);
            infoVo.setInsertList(insertListPage);
            infoVo.setFailList(null);
        }
        else {
            mwPage.setTotal(-1);
            List<ScanResultFailInfo> ret = process(assetsScanResultInfo.getFailList(), ScanResultFailInfo.class, searchParam);
            infoVo.setFailList(ret);
            infoVo.setNoInsertList(null);
            infoVo.setInsertList(null);
        }

        return infoVo;
    }

    private <T> List<T> process(List<T> datas, Class<T> tClass, AssetsInstanceScanSearchParam searchParam) {
        List<T> ret = null;
        if (null != searchParam && null != searchParam.getPage()) {
            MWPage mwPage = searchParam.getPage();
            //如果没有设置总数,说明list的大小为总数
            if (mwPage.getTotal() < 0) {
                int count = (int) datas.size();
                mwPage.setTotal(count);
                int start = (int) CMDBTool.getPageStart(mwPage.getCurrent(), mwPage.getSize());
                int end = start + mwPage.getSize();
                if (end > count) {
                    end = count;
                }
                ret = datas.subList(start, end);
            }
        }
        return ret;
    }
}
